package com.kfdx.dc.controller;

import com.kfdx.dc.common.annotation.Log;
import com.kfdx.dc.common.core.controller.BaseController;
import com.kfdx.dc.common.core.domain.AjaxResult;
import com.kfdx.dc.common.core.page.TableDataInfo;
import com.kfdx.dc.common.enums.BusinessType;
import com.kfdx.dc.common.utils.DateUtils;
import com.kfdx.dc.common.utils.QrCodeUtils;
import com.kfdx.dc.common.utils.StringUtils;
import com.kfdx.dc.common.utils.poi.ExcelUtil;
import com.kfdx.dc.domain.DcTableInfo;
import com.kfdx.dc.service.IDcTableInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 桌码管理Controller
 *
 * @author cx
 * @date 2024-09-20
 */
@Api("桌码管理")
@RestController
@RequestMapping("/dc/info")
public class DcTableInfoController extends BaseController
{
    @Autowired
    private IDcTableInfoService dcTableInfoService;

    /**
     * 查询桌码管理列表
     */
    @ApiOperation("查询桌码管理列表")
    @PreAuthorize("@ss.hasPermi('dc:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(DcTableInfo dcTableInfo)
    {
        startPage();
        List<DcTableInfo> list = dcTableInfoService.selectDcTableInfoList(dcTableInfo);
        return getDataTable(list);
    }

    /**
     * 导出桌码管理列表
     */
    @ApiOperation("导出桌码管理列表")
    @PreAuthorize("@ss.hasPermi('dc:info:export')")
    @Log(title = "桌码管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DcTableInfo dcTableInfo)
    {
        List<DcTableInfo> list = dcTableInfoService.selectDcTableInfoList(dcTableInfo);
        ExcelUtil<DcTableInfo> util = new ExcelUtil<DcTableInfo>(DcTableInfo.class);
        util.exportExcel(response, list, "桌码管理数据");
    }

    /**
     * 获取桌码管理详细信息
     */
    @ApiOperation("获取桌码管理详细信息")
    @PreAuthorize("@ss.hasPermi('dc:info:query')")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") Long tableId)
    {
        return success(dcTableInfoService.selectDcTableInfoByTableId(tableId));
    }

    /**
     * 新增桌码管理
     */
    @ApiOperation("新增桌码管理")
    @PreAuthorize("@ss.hasPermi('dc:info:add')")
    @Log(title = "桌码管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DcTableInfo dcTableInfo)
    {
        dcTableInfo.setCreateTime(DateUtils.getNowDate());
        return toAjax(dcTableInfoService.insertDcTableInfo(dcTableInfo));
    }

    /**
     * 修改桌码管理
     */
    @ApiOperation("修改桌码管理")
    @PreAuthorize("@ss.hasPermi('dc:info:edit')")
    @Log(title = "桌码管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DcTableInfo dcTableInfo)
    {
        dcTableInfo.setUpdateTime(DateUtils.getNowDate());
        return toAjax(dcTableInfoService.updateDcTableInfo(dcTableInfo));
    }

    /**
     * 删除桌码管理
     */
    @ApiOperation("删除桌码管理")
    @PreAuthorize("@ss.hasPermi('dc:info:remove')")
    @Log(title = "桌码管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable Long[] tableIds)
    {
        return toAjax(dcTableInfoService.deleteDcTableInfoByTableIds(tableIds));
    }

    /**
     * 生成二维码
     */
    @ApiOperation("生成二维码")
    @PreAuthorize("@ss.hasPermi('dc:info:qrCode')")
    @Log(title = "生成二维码", businessType = BusinessType.OTHER)
    @GetMapping("/qrCode")
    public AjaxResult qrCode(@RequestBody DcTableInfo dcTableInfo)
    {
        String path = QrCodeUtils.makeQrCode(dcTableInfo.getDeptId(),dcTableInfo.getTableId());
        if(StringUtils.isNotEmpty(path)){

        return success(path);
        }else{
            return error("二维码生成失败，请联系管理员");
        }
    }
}
